package com.face;


import lombok.Data;

import org.junit.Test;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * description:
 *
 * @author 扫地生_saodisheng
 * @date 2021/7/23
 */
public class InnerInterface {

    /**
     * print
     * 打印User信息
     * @param user user
     * @param userConsumer userConsumer
     * @return void
     * @author saodisheng_liuxingwu
     * @date 2021/7/23
     */
    public void print(User user, Consumer<User> userConsumer) {
        userConsumer.accept(user);
    }

    /**
     * getUser
     * 返回一个User
     * @param userSupplier userSupplier
     * @return top.saodisheng.lambdaapi.User
     * @author saodisheng_liuxingwu
     * @date 2021/7/23
     */
    public User getUser(Supplier<User> userSupplier) {
        return userSupplier.get();
    }

    /**
     * transformUser
     * 转换一个User
     * @param user user
     * @param function function
     * @return top.saodisheng.lambdaapi.User
     * @author saodisheng_liuxingwu
     * @date 2021/7/23
     */
    public User transformUser(User user, Function<User, User> function) {
        return function.apply(user);
    }

    /**
     * checkUser
     * 检验User是否合法
     * @param user user
     * @param predicate predicate
     * @return boolean
     * @author saodisheng_liuxingwu
     * @date 2021/7/23
     */
    public boolean checkUser(User user, Predicate<User> predicate) {
        return predicate.test(user);
    }

    @Test
    public void test1() {
        User userObj = new User();
        userObj.setUserName("技术扫地生");
        userObj.setAge(3);

        // 测试Consumer
        InnerInterface mainInst = new InnerInterface();

        mainInst.print(userObj, user -> System.out.println(user));
        mainInst.print(userObj, user -> System.out.println(user));

        // 测试 Supplier
        User user1 = mainInst.getUser(() -> {
            User user = new User();
            user.setUserName("楼上老刘");
            user.setAge(3);
            return user;
        });
        System.out.println(user1);

        // 将技术扫地生的年龄改为10岁
        User user2 = mainInst.transformUser(userObj, user -> {
            user.setAge(10);
            return user;
        });
        System.out.println(user2);

        // 判断User是否为技术扫地生
        boolean checkUser = mainInst.checkUser(userObj, user -> "技术扫地生".equals(user.getUserName()));
        System.out.println(checkUser);
    }
    /**
     * description:
     *
     * @author 扫地生_saodisheng
     * @date 2021/7/23
     */
    @Data
    public class User {
        private String userName;
        private int age;
    }
}
